<?php

namespace Common\Service\ImbaLog;
use Common\Service\ImbaLog\LogBaseService;

// +----------------------------------------------------------------------
// | 文件 LogSourceService.class.php
// +----------------------------------------------------------------------
// | 说明 原日志数据表
// +----------------------------------------------------------------------
// | 
// +----------------------------------------------------------------------
// | Author: 谢赤心 Legend. <xcx_legender@qq.com>
// +----------------------------------------------------------------------

Class LogSourceService extends LogBaseService
{

    public function calculteFromLog($date)
    {

        $total = $this->table("imba_log_source_{$date}")->count();
        $per = 50000;
        $pages = ceil($total/$per);
        $result = [];
        for ($i=1; $i <= $pages; $i++) { 
            $res = $this->table("imba_log_source_{$date}")->limit($per)->page($i)->select();
            if ($res)
            {
                foreach ($res as $data) {
                    $this->workerVideo($data, $result);
                    $this->workerUser($data, $result);
                    $this->workerApp($data, $result);
                }
            }
        }

        // $res = $this->table("imba_log_source_{$date}")->select();
        // foreach ($res as $data) {
        //     $this->workerVideo($data, $result);
        //     $this->workerUser($data, $result);
        //     $this->workerApp($data, $result);
        // }

        $this->calculteReten($date, $result);

        // 计算//
        return $result;
    }

    // 计算当日留存
    public function calculteReten($date, &$result)
    {
        static $retMacs;
        static $totals;

        $platform = 5;

        empty($totals) && $totals = $this->table("imba_mac_total")->getField('mac', true);
        // echo "TOTAL MACS: ";
        // print_r($totals);
        // echo $this->_sql();
        
        empty($retMac[$date]) &&  $retMac[$date] = array_unique($this->table("imba_mac_daily_{$date}")->getField('mac', true));

        $todayNewMacDB = array_unique($this->table("imba_mac_new_{$date}")->getField('mac', true));
      
        $yesdate = date('Ymd', strtotime("{$date} -1 day" ));
        // empty($retMac[$yesdate]) && $retMac[$yesdate] = $this->table("imba_mac_daily_{$yesdate}")->getField('mac', true);

        $today  = $retMac[$date];
        // $yestoday = $retMac[$yesdate];
        $yesNewMac = $this->table("imba_mac_new_{$yesdate}")->getField('mac', true) ;//$totals ? array_diff($yestoday, $totals) : $yestoday;

        // 昨天新增的mac
        $todayNewMac    = $todayNewMacDB ?: ( $totals ? array_diff($today, $totals) : $today );         

        // 今日新增
        if (!$todayNewMacDB){
            foreach ($todayNewMac as $mac) {
                $this->table("imba_mac_new_{$date}")->add(['mac' => $mac]);
            }
        }
        
        $toRetunMac     = array_intersect($yesNewMac, $today); // 昨日新增在今日登录 

        $result['mac'][$platform]['new'] = count($todayNewMac);
        $result['mac'][$platform]['active'] = count($today);
        $result['mac'][$platform]['yesnew'] = count($yesNewMac);
        $result['mac'][$platform]['reten'] = count($yesNewMac) > 0 ? count($toRetunMac) / count($yesNewMac) : 0;

        // print_r($todayNewMac);
        foreach ($todayNewMac as $mac) {
            $res = $this->table("imba_mac_total")->add(['mac' => $mac]);
            $totals[] = $mac;
        }
        // exit;
    }
 
    protected function workerVideo(&$data, &$result)
    {
        $platform = (int)$data['platform'];
        if ($data['object_type'] == 'video'){
            $class = $data['object_class'];
            $result['client'][$platform]['video'][$class]+=$data['step'];
            $result['client']['total']['video'][$class]+=$data['step'];
            if (!empty($data['extend']['appid'])){
                $result['appid'][$data['extend']['appid']]['video'][$class]+= $data['step'];
            }
            if (!empty($data['extend']['from'])){
                $result['from'][$data['extend']['from']]['video'][$class]+= $data['step'];
            }
        }
    }

    protected function workerUser(&$data, &$result)
    {
        $platform = (int)$data['platform'];
        if ($data['object_type'] == 'user'){
            $class = $data['object_class'];
            $result['client'][$platform]['user'][$class]+=$data['step'];
            $result['client']['total']['user'][$class]+=$data['step'];
        }
    }

    protected function workerApp(&$data, &$result)
    {
        $platform = (int)$data['platform'];
        if ($data['object_type'] == 'app'){
            $class = $data['object_class'];
            $result['client'][$platform]['app'][$class]+=$data['step'];
            $result['client']['total']['app'][$class]+=$data['step'];
        }
    }

    protected function workerUserAction(&$data, &$result)
    {
        $platform = (int)$data['platform'];
        if ($data['object_type'] == 'user_action'){
            if ($data['extend']){
                foreach ($data['extend'] as $key => $count) {
                    $result['client'][$platform]['user_action'][$key]+=$count;
                    $result['client']['total']['user_action'][$key]+=$count;
                }
            }
           
        }
    }


}